package com.relytosoft.mizar.media.system.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.system.api.entity.VidAlarmType;
import com.relytosoft.mizar.media.system.api.pojo.dto.VidAlarmTypeDto;
import com.relytosoft.mizar.media.system.services.service.VidAlarmTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
 * 告警类型
 *
 * @Author SunLi
 * @Date: 2022/4/7 11:49
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("/system/vidalarmtype")
@Api(tags="告警类型相关接口")
public class VidAlarmTypeController {
    @Resource
    private VidAlarmTypeService vidAlarmTypeService;

    @GetMapping(value = "/pagelist")
    @SaCheckPermission("system:vidalarmtype:list")
    @ApiOperation("获得告警类型列表信息")
    public ResponseResult<Map<String,Object>> selectVidAlarmTypePageList(PageQuery pageQuery, VidAlarmTypeDto vidAlarmType) {
        VidAlarmType entity= BeanCopyUtils.copy(vidAlarmType,VidAlarmType.class);
        return ResponseResult.ok(CommonUtils.getPageData(vidAlarmTypeService.selectVidAlarmTypePageList(pageQuery,entity)));
    }

    @GetMapping(value = "/list")
    @SaCheckPermission("system:vidalarmtype:list")
    @ApiOperation("获得告警类型列表信息")
    public ResponseResult<List<VidAlarmType>> selectVidAlarmTypeList() {
        LambdaQueryWrapper<VidAlarmType> lw = Wrappers.lambdaQuery();
        lw.eq(VidAlarmType::getStatus,1);
        return ResponseResult.ok(vidAlarmTypeService.list(lw));
    }

    @PostMapping(value = "/insert")
    @SaCheckPermission("system:vidalarmtype:insert")
    @ApiOperation("添加告警类型信息")
    @OpLog(msg = "添加告警类型信息", opType = OpTypeEnum.INSERT)
    public ResponseResult<Void> insertVidAlarmType(@Valid @RequestBody VidAlarmTypeDto vidAlarmType) {
        VidAlarmType entity= BeanCopyUtils.copy(vidAlarmType,VidAlarmType.class);
        entity.setId(null);
        String mes = validate(entity);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        entity.setStatus(1);
        boolean result = vidAlarmTypeService.save(entity);
        if (result) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @PutMapping(value = "/update")
    @SaCheckPermission("system:vidalarmtype:update")
    @ApiOperation("修改告警类型信息")
    @OpLog(msg = "修改告警类型信息", opType = OpTypeEnum.UPDATE)
    public ResponseResult<Void> updateVidAlarmType(@Valid @RequestBody VidAlarmTypeDto vidAlarmType) {
        VidAlarmType entity= BeanCopyUtils.copy(vidAlarmType,VidAlarmType.class);
        String mes = validate(entity);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        boolean result = vidAlarmTypeService.updateById(entity);
        if (result) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @PutMapping(value = "/updatestatus/{id}/{status}")
    @SaCheckPermission("system:vidalarmtype:updatestatus")
    @ApiOperation("修改告警类型信息状态")
    @OpLog(msg = "修改告警类型信息状态", opType = OpTypeEnum.SET)
    public ResponseResult<Void> setVidAlarmTypeStatus(@PathVariable int id,@PathVariable int status) {
        int result = vidAlarmTypeService.updateVidAlarmTypeStatus(id, status);
        if (result>0) {
            return ResponseResult.ok();
        }
        return ResponseResult.fail();
    }

    @GetMapping(value = "/{id}")
    @SaCheckPermission("system:vidalarmtype:getinfo")
    @ApiOperation("获得告警类型信息")
    public ResponseResult<VidAlarmType> getVidAlarmType(@PathVariable int id){
        return ResponseResult.ok(vidAlarmTypeService.getById(id));
    }

    private String validate(VidAlarmType entity) {
        if(entity.getAlarmCategory()==null){
            return "告警类型类别不能为空！";
        }
        if(StrUtil.isEmpty(entity.getName())){
            return "告警类型名称不能为空！";
        }
        if(entity.getAlarmLevel()==null){
            return "告警等级不能为空！";
        }
        return "";
    }
}
